<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="detail">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${detail.categories[1].id}`">{{
          detail.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/${detail.categories[0].id}`">{{
          detail.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ detail.name }}</XtxBreadItem>
      </XtxBread>

      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
            <GoodsImage :images='detail.mainPictures'/>
            <GoodsSales />
        </div>
        <div class="spec">
            <!-- 名字区组件 -->
            <GoodsName :goods='detail' />

            <!-- 规格组件 -->
            <!-- 这里注入skuid可以实现默认选中功能 -->
            <GoodsSku @update-info='updateInfo' :specs='detail.specs' :skus='detail.skus' />

            <!-- 选择数量组件 -->
            <XtxNumbox v-model="num" :inventory="detail.inventory"></XtxNumbox>

            <!-- 按钮组件 -->
            <XtxButton type="primary" style="margin-top:20px;">加入购物车</XtxButton>

        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId='detail.id'  />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
              <GoodsTags/>
          </div>
          <!-- 注意事项 -->
          <div class="goods-warn">
              <GoodsWarn/>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
             <GoodsHot :goodsId="detail.id" :type="1" />
             <GoodsHot :goodsId="detail.id" :type="2" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodsWarn from './components/goods-warn'
import GoodsHot from './components/goods-hot'
import GoodsRelevant from './components/goods-relevant'
import { ref, watch, provide } from 'vue'
import { findGoods } from '@/api/product.js'
import { useRoute } from 'vue-router'
import GoodsSales from './components/goods-sales'
import GoodsName from './components/goods-name'
import GoodsImage from './components/goods-image.vue'
import GoodsSku from './components/goods-sku'
import GoodsTags from './components/goods-tabs.vue'
const useGoods = () => {
  const route = useRoute()
  // 调用接口获取商品详情数据
  const detail = ref(null)
  watch(
    () => route.params.id,
    (newVal) => {
      // 必须保证监听的是商品详情地址
      if (route.fullPath !== '/product/' + newVal) return
      findGoods(newVal).then((ret) => {
        detail.value = ret.result
      })
    },
    {
      immediate: true
    }
  )

  return detail
}
export default {
  name: 'XtxGoodsPage',
  components: { GoodsRelevant, GoodsImage, GoodsSales, GoodsName, GoodsSku, GoodsTags, GoodsHot, GoodsWarn },
  setup () {
    // 定义一个商品选择的数量
    const num = ref(1)
    const detail = useGoods()

    // 给后代组件注入数据,商品详情和商品评论使用
    provide('goods', detail)

    // A1 - 处理子组件传过来的商品相关参数
    const updateInfo = (info) => {
      console.log(info, 1560007)
      // 1 -如果下列几项都有值
      if (info.price && info.oldPrice && info.inventory) {
        // 2 - 获取子组件传递的规格选中后的商品信息,赋值给detail
        detail.value.price = info.price
        detail.value.oldPrice = info.oldPrice
        detail.value.inventory = info.inventory
      }
    }

    return { detail, updateInfo, num }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
